package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.constant.SysCtrlCfgConstant;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.web.core.framework.constant.SysOrgConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.dto.SysScoreTmplDTO;
import cn.fl.system.service.ISysScoreApiService;
import cn.fl.system.service.ISysScoreTmplService;
import cn.fl.system.transform.SysScoreTmplVOTransform;
import cn.fl.system.vo.SysScoreTmplVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 评分模板表 客户端服务层接口实现
 * 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 批量：batch* 查询对象：select* get* 查询集合：search*
 */
@Service
public class SysScoreTmplServiceImpl implements ISysScoreTmplService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysScoreTmplServiceImpl.class);
	
	/**
	 * 系统评分模版 API服务
	 */
	@Autowired
	private ISysScoreApiService sysScoreApiService;
	
	/**
	 * 组织架构API
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;
	
	

	@Override
	public SysScoreTmplVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		//TODO 业务逻辑
		SysScoreTmplDTO dto =this.sysScoreApiService.getTmplInfoById(id);
		
		return SysScoreTmplVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysScoreTmplVO entity) throws BaseException{
		if(null == entity)return null;
		//return this.sysScoreTmplApiService.add(SysScoreTmplVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return this.sysScoreApiService.addSysScoreTmpl(SysScoreTmplVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		List<Long> ids=new ArrayList<Long>() ;
		ids.add(id);
		//return this.sysScoreTmplApiService.removeById(id);
		//TODO 业务逻辑
		return this.sysScoreApiService.removeSysScoreTmpl(ids);
	}

	@Override
	public boolean modify(SysScoreTmplVO entity) throws BaseException{
		if(null == entity)return false;
		//return this.sysScoreTmplApiService.modify(SysScoreTmplVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return this.sysScoreApiService.modifySysScoreTmpl(SysScoreTmplVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
	
		//TODO 业务逻辑
		return this.sysScoreApiService.searchSysScoreTmplPage(page);
	}

	@Override
	public List<SysScoreTmplVO> searchScoreTmplList(String sysCd, String orgCd) {
		if(StringUtils.isEmpty(sysCd) || StringUtils.isEmpty(orgCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码、组织架构不能为空!");
		}
		List<SysScoreTmplDTO> sysScoreTmplDTOList = this.sysScoreApiService.searchScoreTmplList(sysCd, orgCd);
		return SysScoreTmplVOTransform.toVOList(sysScoreTmplDTOList);
	}
	
    /**
     * 事业部集合
     * @return <KEY,VALUE>集合
     * @throws BaseException
     */
	@Override
	public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
		List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isEmpty(list)){
			return kvList;
		}
		for (SysOrgDTO sysOrgDTO : list) {
			KeyValueVO kv = new KeyValueVO();
			kv.setKey(sysOrgDTO.getOrgCd());
			kv.setValue(sysOrgDTO.getOrgNm());
			kvList.add(kv);
		}
		return kvList;
	}
	
	@Override
	public JSONArray searchParentAndCurrentAllsOrgTree(Long currentOrgId,String cstMgrOrgCd) {
		List<Long> idList = new ArrayList<Long>();
		List<SysOrgDTO> orgList = new ArrayList<SysOrgDTO>();
		JSONArray jsonArray = new JSONArray ();
		String currOrg = null;
		if(StringUtils.isNotBlank(cstMgrOrgCd)){
			currOrg = cstMgrOrgCd;
			SysOrgDTO sys = this.sysOrgApiService.selectSysOrgByOrgCd(cstMgrOrgCd,CurrentThreadContext.getCurrentSysCd());
			if(null!=sys){
				idList.add(sys.getId());
			}
		}else{
			currOrg = CurrentThreadContext.getCurrentOrgCd();
			idList.add(currentOrgId);
		}
			//当前及当前一下的组织机构
			List<SysOrgDTO> selfAndChild = 	 this.sysOrgApiService.searchRecuOrgWithchildBy(idList, CurrentThreadContext.getCurrentSysCd());
			List<SysOrgDTO> parentOrg = new ArrayList<SysOrgDTO>();
			if(!currOrg.equals((SysOrgConstant.SYS_ORG_ORG_CD_INIT).toString())){
				int subLength = SysCtrlCfgConstant.SYS_FILE_ORG_CD_GEN_LENGTH;//组织架构每个部分串的长度
				int currOrgLength = currOrg.length();
				while(currOrgLength>subLength){
					currOrgLength = currOrgLength - subLength;
					String tempOrgCd = currOrg.substring(0, currOrgLength);
					SysOrgDTO orgCd = 	sysOrgApiService.selectSysOrgByOrgCd(tempOrgCd,CurrentThreadContext.getCurrentSysCd());
					parentOrg.add(orgCd);
				}
				orgList.addAll(parentOrg);
			}
			orgList.addAll(selfAndChild);
			for (SysOrgDTO sysOrgDTO : orgList) {
				JSONObject obj = new JSONObject();
				obj.put("ischecked", false);
				obj.put("id", sysOrgDTO.getId());
				obj.put("text", sysOrgDTO.getOrgNm());
				obj.put("pid", sysOrgDTO.getOrgPid());
				obj.put("orgCd", sysOrgDTO.getOrgCd());
				obj.put("orgDispNm", sysOrgDTO.getOrgDispNm());
				jsonArray.add(obj);
			}
			JSONArray treeList = this.treeList(jsonArray,SysOrgConstant.SYS_ORG_CD_PID_INIT);
			return treeList;
	}
	private JSONArray treeList(JSONArray treeList, Long parentId) {  
	       JSONArray childMenu = new JSONArray();  
	       for (Object object : treeList) {  
	           JSONObject jsonMenu = JSONObject.fromObject(object);  
	           long oid = jsonMenu.getLong("id"); 
	           long pid = jsonMenu.getLong("pid"); 
	           if (parentId == pid) {  
	               JSONArray c_node = treeList(treeList, oid);  
	               jsonMenu.put("children", c_node);  
	               childMenu.add(jsonMenu);  
	           }  
	       }  
	       return childMenu;  
	   }
	
	@Override
	public List<SysScoreTmplVO> getTmplInfoByNm(String tmplNm) throws BaseException {
		if(StringUtils.isNotBlank(tmplNm)){
			return SysScoreTmplVOTransform.toVOList(this.sysScoreApiService.getTmplInfoByNm(tmplNm));
		}
		return null;
	}
}
